'use strict';

angular.module('core').controller('HomeController', ['$scope', 'homeService',
    function($scope, homeService) {
        // This provides Authentication context.

        $scope.ios_arrow = 'ios-arrow-up.svg';
        $scope.ios_arrow2 = 'ios-arrow-up.svg';
        $scope.ios_arrow3 = 'ios-arrow-up.svg';
        $scope.ios_arrow4 = 'ios-arrow-up.svg';

        var col7_div = document.getElementById('col7_div');
        var col7_header1 = document.getElementById('col7_header1');
        var col7_body1 = document.getElementById('col7_body1');
        var col7_body1_header = document.getElementById('col7_body1_header');
        var col7_header2 = document.getElementById('col7_header2');
        var col7_body2 = document.getElementById('col7_body2');
        var col7_body2_header = document.getElementById('col7_body2_header');
        var bardom = document.getElementById('bardom');
        var bardom2 = document.getElementById('bardom2');

        var col5_div = document.getElementById('col5_div');
        var col5_header1 = document.getElementById('col5_header1');
        var col5_body1 = document.getElementById('col5_body1');
        var col5_header2 = document.getElementById('col5_header2');
        var col5_body2 = document.getElementById('col5_body2');

        

        //图表是否展开
        var bar_top_open = true;
        var bar_bottom_open = true;
        //列表是否展开
        var list_top_open = true;
        var list_bottom_open = true;

        //左侧列高
        var col7_height = col7_div.offsetHeight;
        //右侧列高
        var col5_height = col5_div.offsetHeight;

        function changeHeight() {
            col7_height = col7_div.offsetHeight;
            //都展开
            if (bar_top_open == true && bar_bottom_open == true) {

                col7_body1.style.height = col7_body2.style.height = (col7_height / 2 - 30 - 20) + 'px';
                bardom.style.height = bardom2.style.height = (col7_height / 2 - 30 - 34 - 20) + 'px';
                // col7_body1.style.height = (col7_body1.offsetHeight - 40) + 'px';
                // bardom.style.height  = (bardom.offsetHeight - 40) + 'px';

            }
            if (bar_top_open == false && bar_bottom_open == true) {
                col7_body1.style.height = 0 + 'px';
                col7_body2.style.height = (col7_height - 30 * 2 - 20) + 'px';
                bardom2.style.height = (col7_height - 30 * 2 - 34 - 20) + 'px';
                
            }
            if (bar_top_open == true && bar_bottom_open == false) {
                col7_body1.style.height = (col7_height - 30 * 2 - 20) + 'px';
                bardom.style.height = (col7_height - 30 * 2 - 34 - 20) + 'px';
                col7_body2.style.height = 0 + 'px';
               
            }
            if (bar_top_open == false && bar_bottom_open == false) {
                col7_body1.style.height = 0 + 'px';
                col7_body2.style.height = 0 + 'px';
            }
        }

        function changeHeight2() {
            col5_height = col5_div.offsetHeight;
            //都展开
            if (list_top_open == true && list_bottom_open == true) {

                col5_body1.style.height = col5_body2.style.height = (col5_height / 2 - 30 - 20) + 'px';

                // col7_body1.style.height = (col7_body1.offsetHeight - 40) + 'px';
                // bardom.style.height  = (bardom.offsetHeight - 40) + 'px';
                

            }
            if (list_top_open == false && list_bottom_open == true) {
                col5_body1.style.height = 0 + 'px';
                col5_body2.style.height = (col5_height - 30 * 2 - 20) + 'px';

            }
            if (list_top_open == true && list_bottom_open == false) {
                col5_body1.style.height = (col5_height - 30 * 2 - 20) + 'px';
                col5_body2.style.height = 0 + 'px';
            }
            if (list_top_open == false && list_bottom_open == false) {
                col5_body1.style.height = 0 + 'px';
                col5_body2.style.height = 0 + 'px';
            }
        }

        function changeHeight3() {
            col7_height = col7_div.offsetHeight;
            //都展开
            if (bar_top_open == true && bar_bottom_open == true) {

                col7_body1.style.height = col7_body2.style.height = (col7_height / 2 - 30 - 20) + 'px';
                bardom.style.height = bardom2.style.height = (col7_height / 2 - 30 - 34 - 20) + 'px';
                // col7_body1.style.height = (col7_body1.offsetHeight - 40) + 'px';
                // bardom.style.height  = (bardom.offsetHeight - 40) + 'px';
                myChart.dispatchAction({
                    type: 'dataZoom',
                    startValue: 0,
                    endValue: 3
                });
                myChart2.dispatchAction({
                    type: 'dataZoom',
                    startValue: 0,
                    endValue: 3
                });

            }
            if (bar_top_open == false && bar_bottom_open == true) {
                col7_body1.style.height = 0 + 'px';
                col7_body2.style.height = (col7_height - 30 * 2 - 20) + 'px';
                bardom2.style.height = (col7_height - 30 * 2 - 34 - 20) + 'px';
                // myChart.dispatchAction({
                //     type: 'dataZoom',
                //     start: 0,
                //     end: 3
                // });
                myChart2.dispatchAction({
                    type: 'dataZoom',
                    startValue: 0,
                    endValue: 7
                });
            }
            if (bar_top_open == true && bar_bottom_open == false) {
                col7_body1.style.height = (col7_height - 30 * 2 - 20) + 'px';
                bardom.style.height = (col7_height - 30 * 2 - 34 - 20) + 'px';
                col7_body2.style.height = 0 + 'px';
                myChart.dispatchAction({
                    type: 'dataZoom',
                    startValue: 0,
                    endValue: 7
                });
            }
            if (bar_top_open == false && bar_bottom_open == false) {
                col7_body1.style.height = 0 + 'px';
                col7_body2.style.height = 0 + 'px';
            }
        }

    
        changeHeight();
        changeHeight2();


        var shownGroup;
        var shownGroup2;
        var shownGroup3;
        var shownGroup4;
        var search_batch_name = '';
        var batch_list = []; //批次列表

        var option = {};

        //每个车间所有批次的产品最大数
        // var sums = [];
        var maxs = [];
        //所有批次所有部门产量最大值
        var data_max = 0;

        function bar_max() {
            for (var j = 0; j < batch_list[0].workshop_list.length; j++) {
                // var sum = 0;
                var max = 0;
                for (var i in batch_list) {
                    var num = batch_list[i].workshop_list[j].num;
                    // sum = sum + batch_list[i].workshop_list[j].num;
                    if (num > max) {
                        max = num;
                    }
                };
                maxs[j] = max;
            };
        }

        // var echarts = require('echarts');

        // 基于准备好的dom，初始化echarts实例

        // var phoneWith = window.innerWidth;
        // var phoneHeight = window.innerHeight;


        // bardom2.style.width = bardom.style.width = (phoneWith) * 0.45 + 'px';
        // bardom2.style.height = bardom.style.height = (phoneHeight) * 0.3 + 'px';

        var placeHoledStyle = {
            normal: {
                barBorderColor: 'rgba(0,0,0,0)',
                color: 'rgba(0,0,0,0)'
            },
            emphasis: {
                barBorderColor: 'rgba(0,0,0,0)',
                color: 'rgba(0,0,0,0)'
            }
        };

        //获取有色bar的数据
        function bar_data(id) {
            var res = [];

            for (var i in batch_list) {
                res[i] = batch_list[i].workshop_list[id].num / (data_max / 100);
            }
            return res;
        }
        //获取无色bar的数据
        function bar_blank_data(id) {
            var res = bar_data(id);
            for (var i in batch_list) {
                res[i] = 100 - res[i];
            }

            return res;
        }
        var colors = ['#72b9e7', '#fdcc65', '#4dd0c8', '#f78ca8'];
        //获取对应颜色值
        function get_color(i) {
            return colors[i];
        }
        //1占1/2时，2占满

        function end_half_or_full(i) {
            var end = 0;
            if (i == 1) {
                end = 3;
            }
            if (i == 2) {
                end = 7;
            }
            return end;
        }

        //生成自己的批次列表(找出每个批次每个部门的最大值)
        function get_batch_list(data) {
            batch_list = [];

            var batchs = data;
            for (var i = 0; i < batchs.length; i++) {
                for (var j in batchs[i]) {
                    if (j != null) {
                        if (j == 'MoLotNum') {
                            // if (batchs[i][j] != null) {
                            batch_list.push({
                                batch_name: batchs[i][j],
                                workshop_list: []
                            });
                            // }
                            // else{
                            //     break;  
                            // }

                        } else {
                            if (batchs[i][j] > data_max) {
                                data_max = batchs[i][j];
                            }
                            // if (batchs[i][j] != null) {
                            batch_list[i].workshop_list.push({
                                    workshop_name: j,
                                    num: batchs[i][j]
                                })
                                // }

                        }
                    }

                }
            };

            for (var i = 0; i < batch_list.length; i++) {
                if (batch_list[i].batch_name == null) {
                    batch_list.splice(i, 1);
                }
                if (batch_list[i].workshop_list.length == 0) {
                    batch_list.splice(i, 1);
                }
                // for (var j = 0; j < batch_list[i].workshop_list.length; j++) {

                //     if(batch_list[i].workshop_list[j])
                // };

            };

        }

        var myChart = echarts.init(bardom);
        var myChart2 = echarts.init(bardom2);

        $scope.search_batch = function(batch_name) {
            myChart.showLoading({
                text: '正在努力的读取数据中...',
            });
            myChart2.showLoading({
                text: '正在努力的读取数据中...',
            });
            homeService.getChartData(batch_name).then(function(data) {
                myChart.hideLoading();
                myChart2.hideLoading();

                get_batch_list(data.data);
                // bar_max();

                option = {
                    tooltip: {
                        trigger: 'axis',
                        axisPointer: { // 坐标轴指示器，坐标轴触发有效
                            type: 'shadow', // 默认为直线，可选为：'line' | 'shadow'
                            axis: 'y'
                        },
                        // formatter: '{b}<br/>{a0}:{c0}%<br/>{a2}:{c2}%<br/>{a4}:{c4}%<br/>{a6}:{c6}%'
                        formatter: function(params) { //一行的数据，每偶次列为彩色条数据，奇次列为白色条数据
                            var str = '';
                            for (var i = 0; i < params.length; i = i + 2) {
                                str = str + params[i].seriesName + ":" + Math.round(params[i].value * (data_max / 100)) + "<br/>"
                            };
                            return params[0].name + "<br/>" + str

                        }
                    },

                    legend: {
                        top: 5,

                        itemWidth: 15,
                        itemHeight: 15,
                        data: function() {
                            var res = [];
                            for (var i in batch_list[0].workshop_list) {
                                res[i] = batch_list[0].workshop_list[i].workshop_name;
                            }
                            return res;
                        }()
                    },
                    //containLabel防止标签溢出容器
                    grid: {
                        left: '4%',
                        right: '40',
                        bottom: '5%',
                        top: '40',
                        containLabel: true
                    },
                    xAxis: [{
                        type: 'value',
                        position: 'top',
                        splitLine: {
                            show: false
                        },
                        axisLabel: {
                            show: false
                        },
                        splitNumber: 3
                    }],
                    yAxis: [{
                        type: 'category',
                        splitLine: {
                            show: false
                        },

                        data: function() {
                            var res = [];
                            for (var i in batch_list) {
                                res[i] = batch_list[i].batch_name;
                            }
                            return res;
                        }()
                    }],
                    dataZoom: {
                        type: 'slider',
                        yAxisIndex: 0,
                        // start: 0,
                        // end: 40
                        startValue: 0,
                        endValue: 3,
                        //如果设置为 true 则锁定选择区域的大小，也就是说，只能平移，不能缩放。
                        zoomLock: true,
                        handleSize: 5,
                        width: 20
                    },
                    series: function() {
                        var res = [];

                        for (var i in batch_list[0].workshop_list) {
                            res.push({
                                name: batch_list[0].workshop_list[i].workshop_name,
                                type: 'bar',
                                stack: '总量',
                                label: {
                                    normal: {
                                        show: true,
                                        position: 'insideLeft',
                                        formatter: function(param) {
                                            return Math.round(param.value * (data_max / 100));
                                        }
                                    }
                                },
                                itemStyle: {
                                    normal: {
                                        color: get_color(i)
                                    }
                                },
                                data: bar_data(i)
                            })
                            res.push({
                                name: batch_list[0].workshop_list[i].workshop_name,
                                type: 'bar',
                                stack: '总量',
                                itemStyle: placeHoledStyle,
                                data: bar_blank_data(i)
                            })

                        }

                        return res;
                    }(),

                    backgroundColor: "#fff"
                };

                // 使用刚指定的配置项和数据显示图表。
                myChart.setOption(option);
                myChart2.setOption(option);
                //window.onresize = myChart.resize;
                //window.onresize = myChart2.resize;
                //解决了只有第二个图自适应的问题
                //angular.element(window).resize(myChart.resize);
                //angular.element(window).resize(myChart2.resize);
                //jquery resize监听 angular.element等同于
                angular.element('#bardom').bind('resize', myChart.resize);
                angular.element('#bardom2').bind('resize', myChart2.resize);

            }, function(res) {
                // popService.alert(res.message || "登陆失败")      
            })
        };
        $scope.search_batch(search_batch_name);

        //         $scope.panels = [
        //   {
        //     "title": "Collapsible Group Item #1",
        //     "body": "Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch."
        //   },
        //   {
        //     "title": "Collapsible Group Item #2",
        //     "body": "Food truck fixie locavore, accusamus mcsweeney's marfa nulla single-origin coffee squid. Exercitation +1 labore velit, blog sartorial PBR leggings next level wes anderson artisan four loko farm-to-table craft beer twee."
        //   },
        //   {
        //     "title": "Collapsible Group Item #3",
        //     "body": "Etsy mixtape wayfarers, ethical wes anderson tofu before they sold out mcsweeney's organic lomo retro fanny pack lo-fi farm-to-table readymade."
        //   }
        // ];
        // $scope.panels.activePanel = -1;

        $scope.toggleGroup = function(group) {
            if ($scope.isGroupShown(group)) {
                bar_top_open = true;
                shownGroup = null;
                $scope.ios_arrow = 'ios-arrow-up.svg';
            } else {
                bar_top_open = false;
                shownGroup = group;
                $scope.ios_arrow = 'ios-arrow-down.svg';
            }
            changeHeight3();
        };
        $scope.isGroupShown = function(group) {
            return shownGroup === group;
        };

        $scope.toggleGroup2 = function(group) {
            if ($scope.isGroupShown2(group)) {
                bar_bottom_open = true;
                shownGroup2 = null;
                $scope.ios_arrow2 = 'ios-arrow-up.svg';
                //  myChart.dispatchAction({
                //  type: 'dataZoom',
                //  start: 0,
                //  end: 7
                // });
            } else {
                bar_bottom_open = false;
                shownGroup2 = group;
                $scope.ios_arrow2 = 'ios-arrow-down.svg';
            }
            changeHeight3();
        };
        $scope.isGroupShown2 = function(group) {
            return shownGroup2 === group;
        };
        $scope.toggleGroup3 = function(group) {
            if ($scope.isGroupShown3(group)) {
                list_top_open = true;
                shownGroup3 = null;
                $scope.ios_arrow3 = 'ios-arrow-up.svg';
            } else {
                list_top_open = false;
                shownGroup3 = group;
                $scope.ios_arrow3 = 'ios-arrow-down.svg';
            }
            changeHeight2();

        };
        $scope.isGroupShown3 = function(group) {
            return shownGroup3 === group;
        };
        $scope.toggleGroup4 = function(group) {
            if ($scope.isGroupShown4(group)) {
                list_bottom_open = true;
                shownGroup4 = null;
                $scope.ios_arrow4 = 'ios-arrow-up.svg';
            } else {
                list_bottom_open = false;
                shownGroup4 = group;
                $scope.ios_arrow4 = 'ios-arrow-down.svg';
            }
            changeHeight2();

        };
        $scope.isGroupShown4 = function(group) {
            return shownGroup4 === group;
        };

        //完工日报数据
        $scope.report_list = [{
            report_id: "1",
            out_batch: "ASD21342",
            in_batch: "ASD21342",
            client_name: "顾家",
            client_num: "300"
        }, {
            report_id: "2",
            out_batch: "ASD21342",
            in_batch: "ASD21342",
            client_name: "新日",
            client_num: "200"
        }, {
            report_id: "3",
            out_batch: "ASD21342",
            in_batch: "ASD21342",
            client_name: "兴旺达",
            client_num: "100"
        }, {
            report_id: "1",
            out_batch: "ASD21342",
            in_batch: "ASD21342",
            client_name: "顾家顾家",
            client_num: "300"
        }, {
            report_id: "2",
            out_batch: "ASD21342",
            in_batch: "ASD21342",
            client_name: "新日新日",
            client_num: "200"
        }, {
            report_id: "3",
            out_batch: "ASD21342",
            in_batch: "ASD21342",
            client_name: "兴旺达",
            client_num: "100"
        }, {
            report_id: "1",
            out_batch: "ASD21342",
            in_batch: "ASD21342",
            client_name: "顾家顾家",
            client_num: "300"
        }, {
            report_id: "2",
            out_batch: "ASD21342",
            in_batch: "ASD21342",
            client_name: "新日新日",
            client_num: "200"
        }, {
            report_id: "3",
            out_batch: "ASD21342",
            in_batch: "ASD21342",
            client_name: "兴旺达",
            client_num: "100"
        }, {
            report_id: "1",
            out_batch: "ASD21342",
            in_batch: "ASD21342",
            client_name: "顾家",
            client_num: "300"
        }, {
            report_id: "2",
            out_batch: "ASD21342",
            in_batch: "ASD21342",
            client_name: "新日",
            client_num: "200"
        }, {
            report_id: "3",
            out_batch: "ASD21342",
            in_batch: "ASD21342",
            client_name: "兴旺达",
            client_num: "112"
        }]

        //质量预警看板数据
        $scope.alert_list = [{
            date: "2016-3-29",
            detail_list: [{
                alert_id: 1,
                alert_batch: "SLOT01",
                alert_time: "09:32",
                alert_work: "备料首件质检作业",
                person: "李敏"
            }, {
                alert_id: 2,
                alert_batch: "SLOT02",
                alert_time: "08:32",
                alert_work: "补土全检作业",
                person: "吴少华"
            }, {
                alert_id: 3,
                alert_batch: "SLOT08",
                alert_time: "07:32",
                alert_work: "备料首件质检作业",
                person: "李敏"
            }, {
                alert_id: 4,
                alert_batch: "SLOT02",
                alert_time: "08:32",
                alert_work: "补土全检作业",
                person: "吴少华"
            }, {
                alert_id: 5,
                alert_batch: "SLOT08",
                alert_time: "07:32",
                alert_work: "备料首件质检作业",
                person: "李敏"
            }]
        }, {
            date: "2016-3-28",
            detail_list: [{
                alert_id: 1,
                alert_batch: "SLOT01",
                alert_time: "09:32",
                alert_work: "备料首件质检作业",
                person: "李敏"
            }, {
                alert_id: 2,
                alert_batch: "SLOT02",
                alert_time: "08:32",
                alert_work: "补土全检作业",
                person: "吴少华"
            }, {
                alert_id: 3,
                alert_batch: "SLOT08",
                alert_time: "07:32",
                alert_work: "备料首件质检作业",
                person: "李敏"
            }, {
                alert_id: 4,
                alert_batch: "SLOT02",
                alert_time: "08:32",
                alert_work: "补土全检作业",
                person: "吴少华"
            }, {
                alert_id: 5,
                alert_batch: "SLOT08",
                alert_time: "07:32",
                alert_work: "备料首件质检作业",
                person: "李敏"
            }]
        }]


        //离开此页面注销
        $scope.$on('$destroy', function() {
            angular.element(window).off('resize', myChart.resize);
            angular.element(window).off('resize', myChart2.resize);
        });
    }

]);